package top.honlnk.czuan.pojo.entity;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Words {
    private Integer id;
    private String word;
    private String baseTranslation;
    private String explain;                         // 单词的解释，或者辅助记忆方法
    private String sense;                           // 单词的含义，比如在牛津字典中的解释
    private String usSymbol;                        // 美式音标
    private String enSymbol;                        // 英式音标
    private Integer isAlone;                        // 是否是独立词
    private Integer superordinateId;                // 上义词

    private String translationsIdList;              // 单词的翻译id的顺序列表

    private String rootsIdList;                     // 单词的词根di的顺序列表
    private String prefixesIdList;                  // 单词的前缀词的id的顺序列表
    private String suffixesIdList;                  // 单词的后缀词的id的顺序列表

    private String rootsTranslationsIdList;         // 单词的根词的翻译id的顺序列表
    private String prefixesTranslationsIdList;      // 单词的前缀词的翻译id的顺序列表
    private String suffixesTranslationsIdList;      // 单词的后缀词的翻译id的顺序列表

    private String dictDataJsonUrl;                 // 词典查询结果JSON文件地址
    private String aiExplainMdUrl;                  // AI讲解单词md文件地址
    private String enAudioUrl;                      // 英文音频文件地址
    private String zhAudioUrl;                      // 中文音频文件地址

    private LocalDate createdAt;
    private LocalDate updateAt;
    private String createdBy;
    private String updateBy;


    // Getter for JSON string
    public JSONObject getTIdsJson() {return JSONUtil.parseObj(this.translationsIdList);}
    public JSONArray getRootsIdsJson() {return JSONUtil.parseArray(this.rootsIdList);}
    public JSONArray getPrefixesIdsJson() {return JSONUtil.parseArray(this.prefixesIdList);}
    public JSONArray getSuffixesIdsJson() {return JSONUtil.parseArray(this.suffixesIdList);}
    public JSONObject getRTIdsJson() {return JSONUtil.parseObj(this.rootsTranslationsIdList);}
    public JSONObject getPFTIdsJson() {return JSONUtil.parseObj(this.prefixesTranslationsIdList);}
    public JSONObject getSFTIdsJson() {return JSONUtil.parseObj(this.suffixesTranslationsIdList);}

    // Setter for JSON string
    public void setTIdsJson(JSONArray json) {
        this.translationsIdList = JSONUtil.toJsonStr(json);
    }
    public void setRootsIdsJson(JSONArray json) {this.rootsIdList = JSONUtil.toJsonStr(json);}
    public void setPrefixesIdsJson(JSONArray json) {this.prefixesIdList = JSONUtil.toJsonStr(json);}
    public void setSuffixesIdsJson(JSONArray json) {this.suffixesIdList = JSONUtil.toJsonStr(json);}
    public void setRTIdsJson(JSONObject json) {
        this.rootsTranslationsIdList = JSONUtil.toJsonStr(json);
    }
    public void setPFTIdsJson(JSONObject json) {
        this.prefixesTranslationsIdList = JSONUtil.toJsonStr(json);
    }
    public void setSFTIdsJson(JSONObject json) {
        this.suffixesTranslationsIdList = JSONUtil.toJsonStr(json);
    }

    // Getter for List<Integer>
    public List<Integer> getTIds() {
        return parseJsonArray(this.translationsIdList);
    }
    public List<Integer> getRootsIds() {
        return parseJsonArray(this.rootsIdList);
    }
    public List<Integer> getPrefixesIds() {
        return parseJsonArray(this.prefixesIdList);
    }
    public List<Integer> getSuffixesIds() {
        return parseJsonArray(this.suffixesIdList);
    }
    public Map<Integer, List<Integer>> getRTIds() {
        return parseJsonMap(this.rootsTranslationsIdList);
    }
    public Map<Integer, List<Integer>> getPFTIds() {
        return parseJsonMap(this.prefixesTranslationsIdList);
    }
    public Map<Integer, List<Integer>> getSFTIds() {
        return parseJsonMap(this.suffixesTranslationsIdList);
    }
    private static List<Integer> parseJsonArray(String jsonArrayStr) {
        if (jsonArrayStr == null || jsonArrayStr.isEmpty()) {
            return List.of();
        }
        JSONArray jsonArray = JSONUtil.parseArray(jsonArrayStr);
        return jsonArray.stream()
                .map(item -> item instanceof Integer ? (Integer) item : Integer.parseInt(item.toString()))
                .collect(Collectors.toList());
    }

    private static Map<Integer, List<Integer>> parseJsonMap(String jsonMapStr) {
        if (jsonMapStr == null || jsonMapStr.isEmpty()) {
            return Map.of();
        }
        JSONObject jsonMap = JSONUtil.parseObj(jsonMapStr);
         return jsonMap.entrySet().stream()
                .collect(Collectors.toMap(
                        entry -> Integer.parseInt(entry.getKey()),
                        entry -> parseJsonArray(entry.getValue().toString())
                ));
    }

    // Setter for List<Integer>
    public void setTIds(List<Integer> list) {
        this.translationsIdList = JSONUtil.toJsonStr(list);
    }
    public void setRootsIds(List<Integer> list) {this.rootsIdList = JSONUtil.toJsonStr(list);}
    public void setPrefixesIds(List<Integer> list) {this.prefixesIdList = JSONUtil.toJsonStr(list);}
    public void setSuffixesIds(List<Integer> list) {this.suffixesIdList = JSONUtil.toJsonStr(list);}
    public void setRTIds(Map<Integer, List<Integer>> map) {
        this.rootsTranslationsIdList = JSONUtil.toJsonStr(map);
    }
    public void setPFTIds(Map<Integer, List<Integer>> map) {
        this.prefixesTranslationsIdList = JSONUtil.toJsonStr(map);
    }
    public void setSFTIds(Map<Integer, List<Integer>> map) {
        this.suffixesTranslationsIdList = JSONUtil.toJsonStr(map);
    }
}
